package hazesoft.Collection;

import java.io.ByteArrayOutputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Collection_Setup extends Activity {
	
	public class TestResults{
		public int UploadError;
		public int MessageError;
		public String Upload;
		public String Message;
	};
	
	Button btnContinue = null;
	
	public void MessageBox(String message, int Length){
	    Toast.makeText(this,message,Length).show();
	}
	
	public void SavePref(String Key, String Value)
	{
		Editor e = this.getSharedPreferences("AHB",MODE_PRIVATE).edit();
		e.putString(Key, Value);
		e.commit();		
	}
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setup);
        
        Button btnTest = (Button)findViewById(R.id.btnTest);
        btnTest.setOnClickListener(btnTest_OnClick);
        
        btnContinue = (Button)findViewById(R.id.btnContinue);
        btnContinue.setOnClickListener(btnContinue_OnClick);
	}
	
	public View.OnClickListener btnTest_OnClick = new View.OnClickListener() {
		
		public void onClick(View arg0) {
			EditText edtMessage = (EditText)findViewById(R.id.edtMessage);
			EditText edtUpload = (EditText)findViewById(R.id.edtServerUpload);
			EditText edtPID = (EditText)findViewById(R.id.edtPhoneID);
			
			String sUpload = edtUpload.getText().toString();
			String sMessage = edtMessage.getText().toString();
			String sPID = edtPID.getText().toString();
			
			if(sPID.length() != 0 && sUpload.length() != 0 && sMessage.length() != 0)
			{	
				ProgressDialog dialog = ProgressDialog.show(Collection_Setup.this, "", 
	                    "Working. Please wait...", true);	
			
				TestResults Res = Test(sUpload, sMessage, sPID);
				dialog.dismiss();
			
						
				if(Res.UploadError != 0 || Res.MessageError != 0) //If Upload or Message address is wrong, Error;
				{
					String Error = "Error with following server(s):\n";
					if(Res.UploadError != 0)
						Error += "Upload Server(" + Res.Upload + ")\n";
					if(Res.MessageError != 0)
						Error += "Message Server(" + Res.Message + ")\n";
					MessageBox(Error, 300);
				}
				else
				{
					btnContinue.setEnabled(true);
				}
			
			
			}
			else
			{
				MessageBox("Please make sure you have filled in all the details.", 600);
				
			}
		}
	};
	
	public View.OnClickListener btnContinue_OnClick = new View.OnClickListener() {
		
		public void onClick(View v) {
			
			EditText edtMessage = (EditText)findViewById(R.id.edtMessage);
			EditText edtUpload = (EditText)findViewById(R.id.edtServerUpload);
			EditText edtPID = (EditText)findViewById(R.id.edtPhoneID);
			SavePref("SERVER_ADDRESS", edtUpload.getText().toString());
			SavePref("PHONE_ID", edtPID.getText().toString());
			SavePref("MESSAGE_ADDRESS", edtMessage.getText().toString());
			SavePref("FIRST", "FALSE");
			Collection_Setup.this.finish();
			
		}
	};
	
	public TestResults Test(String Upload, String Message, String PID)
	{
		TestResults Res =  new TestResults();
		try
		{
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(new HttpGet(Upload + "?Register=" + PID + "&Type=Register"));
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			response.getEntity().writeTo(out);
			if(out.toString().contains("OK") != true)
			{
				Res.UploadError = -1;
				Res.Upload = out.toString();
			}
			else
			{
				Res.UploadError = 0;
			}
			
		}
		catch(Exception ex)
		{
			Res.UploadError = -1;
			Res.Upload = "Error connecting to server";
		}
		
		try
		{
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(new HttpGet(Message + "?Register=" + PID + "&Type=Register"));
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			response.getEntity().writeTo(out);
			
			if(out.toString().contains("OK") != true)
			{
				Res.MessageError = -1;
				Res.Message = out.toString();
			}
			else
			{
				Res.MessageError = 0;
			}
			
		}
		catch(Exception ex)
		{
			Res.MessageError = -1;
			Res.Message = "Error connecting to server";
		}
		
		return Res;
	}

}
